package com.wencst.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wencst.security.bean.User;
import com.wencst.security.handler.UserService;

import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
public class RegistryController {

	@Autowired
	PasswordEncoder passwordEncoder;

	@Autowired
	UserService userService;
	
	  @PostMapping("/user/registry")
	  public String login(@RequestParam String username, @RequestParam String password) {
		  log.info("-----post page----- user registry "+username);
		  Map<String,Object> users = null;
		  User user ;
		  try {
			  users = userService.getUserMap();
			  user = userService.getUserByName(username);
			  return "error";
		  } catch (UsernameNotFoundException e) {
			  user = new User();
			  user.setUsername(username);
			  user.setPassword(password);
			  user.setRoles("admin");
			  if (users == null) {
				  users = new HashMap<String,Object>();
				  users.put(username, user);
			  }
		  }
		  return "ok";
	  }
}
